//
//  SDHotCityTableViewCell.swift
//  BotuPanoram
//
//  Created by slowdony on 2019/11/20.
//  Copyright © 2019 Bicon. All rights reserved.
//

import UIKit
import SwiftyJSON



class SDHotCityTableViewCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource {
    
    var dataArr: Array<JSON>?
    @IBOutlet weak var collectionView: UICollectionView!
    var isDepart=true
    var dismissVC:(()->Void)?
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = UICollectionView.ScrollDirection.vertical
        flowLayout.minimumLineSpacing = 12
        flowLayout.minimumInteritemSpacing = 20
        flowLayout.itemSize = CGSize(width: (kScreenW-49-40-16)/3, height: 28)
        flowLayout.sectionInset = UIEdgeInsets(top: 10, left: 16, bottom: 0, right: 49)
        
        
        collectionView.collectionViewLayout=flowLayout
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib.init(nibName: "SDCitySelectCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "SDCitySelectCollectionViewCell")
        collectionView.backgroundColor = UIColor.white
        
        
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    
    

}



// MARK: class func
extension SDHotCityTableViewCell {
    
  
    class func cellHeight(data:Array<JSON>) -> CGFloat {
        let height = (data.count+2)/3*(28+12)+12
        return CGFloat(height)
    }
}


// MARK: UICollectionViewDelegate
extension SDHotCityTableViewCell {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArr?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView .dequeueReusableCell(withReuseIdentifier: "SDCitySelectCollectionViewCell", for: indexPath) as! SDCitySelectCollectionViewCell
        cell.name.text=dataArr?[indexPath.row].string
        return  cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let cell:SDCitySelectCollectionViewCell = collectionView.cellForItem(at: indexPath) as! SDCitySelectCollectionViewCell
        
        Network.request(.getCityCodeByName(["name":cell.name.text ?? ""])) { JSON in
           let data = JSON["data"].array
            if data?.count ?? 0 > 0{
                let code =  data?[0]["code"].string ?? ""
                let dic=["cityName":cell.name.text ?? "","cityCode":code]

                if self.isDepart {
                    
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "chooseDepart"), object: dic, userInfo: nil)
                    
                }else{
                    
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "chooseArrive"), object: dic, userInfo: nil)

                }
                if self.dismissVC != nil {
                    self.dismissVC!()
                }
                
                
            }
        } failure: { err in
            
        }

        
    }
    
 
   
    
}
